<template>
    <div>
        <!--面包屑导航-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!---->
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="dialogAddShow">添加分类</el-button>
                </el-col>
            </el-row>
            <tree-table
                :data="cateList"
                :columns="columns"
                tree-type
                :selection-type="false"
                :expand-type="false"
                show-index
                index-text="序号"
                border
                class="tree-table"
            >
                <template slot="isOK" slot-scope="scope">
                    <i v-if="!scope.row.cat_deleted" class="el-icon-success" style="color:green"></i>
                    <i v-else class="el-icon-error" style="color:red"></i>
                </template>
                <template slot="sort" slot-scope="scope">
                    <el-tag size="mini" v-if="scope.row.cat_level == 0" type="primary">一级</el-tag>
                    <el-tag size="mini" v-else-if="scope.row.cat_level == 1" type="success">二级</el-tag>
                    <el-tag size="mini" v-else type="warning">三级</el-tag>
                </template>
                <template slot="opt" slot-scope="scope">
                    <el-button
                        type="primary"
                        icon="el-icon-edit"
                        size="mini"
                        @click="dialogEditShow(scope.row.cat_id, scope.row.mg_state)"
                        >编辑</el-button
                    >
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row.cat_id)"
                        >删除</el-button
                    >
                </template>
            </tree-table>

            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            ></el-pagination>
        </el-card>
        <!--添加分类弹框-->
        <el-dialog title="添加分类" :visible.sync="dialogAddVisible" @close="dialogAddClose" width="500px">
            <el-form :model="newCate" :rules="newCateRules" ref="newCateRef" :label-width="formLabelWidth">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="newCate.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类">
                    <el-cascader
                        sizs="mini"
                        v-model="cascaderVal"
                        :options="parentCateList"
                        :props="cascaderProps"
                        @change="cascaderChange"
                        clearable
                        change-on-select
                    ></el-cascader>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAddVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCate">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="编辑分类信息" :visible.sync="dialogEditVisible" @close="dialogEditClose" width="50%">
            <el-form :model="editCate" :rules="newCateRules" ref="editCateRef" :label-width="formLabelWitdh">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="editCate.cat_name"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogEditVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleEdit()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            queryInfo: {
                type: 3,
                pagenum: 1,
                pagesize: 10
            },
            cateList: [],
            parentCateList: [],
            total: 0,
            columns: [
                {
                    label: '分类名称',
                    prop: 'cat_name'
                },
                {
                    label: '是否有效',
                    type: 'template',
                    template: 'isOK'
                },
                {
                    label: '排序',
                    type: 'template',
                    template: 'sort'
                },
                {
                    label: '操作',
                    type: 'template',
                    template: 'opt'
                }
            ],
            cascaderProps: {
                expandTrigger: 'hover',
                label: 'cat_name',
                value: 'cat_id'
            },
            cascaderVal: [],
            newCate: {
                cat_pid: 0,
                cat_name: '',
                cat_level: 0
            },
            newCateRules: {
                cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
            },
            editCate: {},
            dialogAddVisible: false,
            dialogEditVisible: false,
            formLabelWidth: '100px'
        }
    },
    created() {
        this.getCateList()
    },
    methods: {
        async getCateList() {
            const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.cateList = res.data.result
            this.total = res.data.total
        },
        handleSizeChange(newSize) {
            // 监听每页条数的改变
            this.queryInfo.pagesize = newSize
            this.getCateList()
        },
        handleCurrentChange(newPage) {
            // 监听页码值改变
            this.queryInfo.pagenum = newPage
            this.getCateList()
        },
        dialogAddShow() {
            this.getParentCateList()
            this.dialogAddVisible = true
        },
        addCate() {
            // 添加商品分类
            this.$refs.newCateRef.validate(async valid => {
                if (!valid) return
                const { data: res } = await this.$http.post('categories', this.newCate)
                if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
                this.$message.success(res.meta.msg)
                this.getCateList()
                this.dialogAddVisible = false
            })
        },
        dialogAddClose() {
            this.$refs.newCateRef.resetFields()
            this.cascaderVal = []
            this.newCate.cat_pid = 0
            this.newCate.cat_level = 0
        },
        dialogEditClose() {
            this.$refs.editCateRef.resetFields()
        },
        async getParentCateList() {
            // 添加分类弹框关闭事件
            const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.parentCateList = res.data
        },
        cascaderChange() {
            if (this.cascaderVal.length > 0) {
                // 如果有选择父级分类
                this.newCate.cat_pid = this.cascaderVal[this.cascaderVal.length - 1]
                this.newCate.cat_level = this.cascaderVal.length
            } else {
                this.newCate.cat_pid = 0
                this.newCate.cat_level = 0
            }
        },
        handleDelete(id) {
            // 删除用户
            this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    const { data: res } = await this.$http.delete('categories/' + id)
                    if (res.meta.status !== 200) {
                        return this.$message.error(res.meta.msg)
                    }
                    this.$message.success(res.meta.msg)
                    this.getCateList()
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
        },
        async dialogEditShow(Id) {
            // 打开并查询分类
            this.dialogEditVisible = true
            const { data: res } = await this.$http.get('categories/' + Id)
            if (res.meta.status !== 200) {
                return this.$message.error(res.meta.msg)
            }
            this.editCate = res.data
        },
        handleEdit() {
            this.$refs.editCateRef.validate(async valid => {
                if (!valid) return
                const { data: res } = await this.$http.put('categories/' + this.editCate.cat_id, {
                    cat_name: this.editCate.cat_name
                })
                if (res.meta.status !== 200) {
                    return this.$message.error(res.meta.msg)
                }
                this.$message.success(res.meta.msg)
                this.dialogEditVisible = false
                this.getCateList()
            })
        }
    }
}
</script>

<style lang="less" scoped>
.tree-table {
    margin-top: 15px;
}
.el-cascader {
    width: 100%;
}
</style>
